package com.lywz.biz.dao.scenic.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.lywz.biz.common.dao.AbstractBaseDao;
import com.lywz.biz.common.entity.scenic.Attraction;
import com.lywz.biz.common.entity.scenic.Scenic;
import com.lywz.biz.common.entity.scenic.ScenicAround;
import com.lywz.biz.common.entity.scenic.ScenicHomepage;
import com.lywz.biz.common.entity.scenic.ScenicImage;
import com.lywz.biz.common.entity.scenic.ScenicVideo;
import com.lywz.biz.dao.scenic.ScenicDao;
import com.lywz.core.util.CommonDefaultCheck;

/**
 * 景区DAO的实例
 * 
 * @author Huadi Luojun
 * @date 2010-5-3
 */
@Repository("scenicDao")
public class ScenicDaoImpl extends AbstractBaseDao implements ScenicDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Scenic> getScenicById(Integer Id) throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicById");
        q.setInteger("id", Id);
        return (List<Scenic>) CommonDefaultCheck.checkDefault(q.list()); // 该方法屏蔽了q.list()为空的情况，自动返回空List。
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Scenic> getScenicList() throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicList");
        return (List<Scenic>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Scenic> getScenicListByCategory(String category) throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicByCategory");
        q.setParameter("category", category);
        return (List<Scenic>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Scenic> getScenicListByCity(String city) throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicByCity");
        q.setParameter("city", city);
        return (List<Scenic>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Scenic> getScenicListByLevel(String level) throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicByLevel");
        q.setParameter("level", level);
        return (List<Scenic>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ScenicHomepage> getHomepageList() throws Exception {
        Query q = session().getNamedQuery("scenic.HomepageList");
        return (List<ScenicHomepage>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    public List<ScenicHomepage> getCategoryList(Integer category) throws Exception {
        Query q = session().getNamedQuery("scenic.CategoryList");
        q.setInteger("category", category);
        return (List<ScenicHomepage>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ScenicAround> getScenicAround(Integer scenic) throws Exception {
        Query q = session().getNamedQuery("scenic.getAroundScenicList");
        q.setParameter("scenicId", scenic);
        return (List<ScenicAround>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Attraction> getAttractionList(Integer scenicId) throws Exception {
        Query q = session().getNamedQuery("scenic.getAttractionList");
        q.setInteger("scenicId", scenicId);
        return (List<Attraction>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ScenicImage> getScenicImage(Integer scenicId) throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicImage");
        q.setInteger("scenicId", scenicId);
        return (List<ScenicImage>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ScenicVideo> getScenicVideo(Integer scenicId) throws Exception {
        Query q = session().getNamedQuery("scenic.getScenicVideo");
        q.setInteger("scenicId", scenicId);
        return (List<ScenicVideo>) CommonDefaultCheck.checkDefault(q.list());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Scenic> getCategoryScenicList(Integer category, Integer city, Integer level)
            throws Exception {

        if (!category.equals(null)) category = -1;
        if (!city.equals(null)) city = -1;
        if (!level.equals(null)) level = -1;

        Criteria c = session().createCriteria(Scenic.class);

        if (!level.equals(-1)) {
            c.add(Restrictions.eq("level", level));
        }
        if (!city.equals(-1)) {
            c.add(Restrictions.eq("city", city));
        }
        if (!category.equals(-1)) {
            c.add(Restrictions.eq("category", category));
        }
        return (List<Scenic>) CommonDefaultCheck.checkDefault(c.list());
    }

}
